package patrol.mobile.com.mobilepatrol.activity.office;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.PiShiAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.PiShiListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

public class LeaderInstructionsActivity extends BaseActivity {

    private static final String TAG = "LeaderInstructionsActivity";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_instrunctions)
    EditText etInstrunctions;
    @BindView(R.id.tv_send)
    TextView tvSend;
    @BindView(R.id.lin_btm)
    LinearLayout linBtm;
    private String id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_leader_instructions;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(LeaderInstructionsActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("领导批示");

    }

    @Override
    public void initData() {
        super.initData();
        id = getIntent().getStringExtra("id");
        requestData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void requestData() {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GETUSERTASKNOTESLIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("taskId", id);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("data", response);
                PiShiListBean piShiListBean = GsonUtil.GsonToBean(response, PiShiListBean.class);
                recyclerView.setLayoutManager(new LinearLayoutManager(LeaderInstructionsActivity.this));
                PiShiAdapter piShiAdapter = new PiShiAdapter(LeaderInstructionsActivity.this);
                recyclerView.setAdapter(piShiAdapter);
                piShiAdapter.addItems(piShiListBean.getList());
            }
        });
    }

    private void updataData() {
        HashMap mainJson = new HashMap();
        mainJson.put("userName", UserManager.getUserName());
        mainJson.put("userId", UserManager.getUser().ryid);
        mainJson.put("notes", etInstrunctions.getText().toString());
        mainJson.put("billDate", TimeUtils.dateString4(System.currentTimeMillis()));
        mainJson.put("taskId", id);
        mainJson.put("id", System.currentTimeMillis());
        HashMap user = new HashMap();
        mainJson.put("user", user);
        user.put("name", UserManager.getUserName());
        user.put("id", UserManager.getUser().ryid);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.USERTASKNOTESUPLOADDATA);
        HashMap<String, String> map = new HashMap<>();
        map.put("mainJson", GsonUtil.mapString2Json(mainJson));
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("data", response);
                etInstrunctions.setText("");
                ToastUtils.showToast("上传成功！");
                requestData();
            }
        });
    }

    @Override
    public void setListener() {

    }


    @OnClick({R.id.tv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send:
                if (!TextUtils.isEmpty(etInstrunctions.getText().toString())) {
                    updataData();
                } else {
                    ToastUtils.showToast("请输入批示内容！");
                }
                break;
        }
    }
}
